<?php
/**
 * @author yupoxiong<i@yufuping.com>
 * @title 公共配置
 */

namespace app\api\controller;
use app\common\model\ShopConfig;
use app\common\server\FileServer;
use app\common\server\UrlServer;
use app\api\logic\AdLogic;
use app\api\logic\ArticleLogic;
use think\Request;
use think\Db;
use think\facade\Log;
use think\facade\Cache;
use think\facade\Env;
class ParameterController extends ApiBase
{

    protected $authExcept = [
        'icon','wz_config','public_config','share_config'
    ];

    //分享配置
    public function share_config(Request $request)
    {
        $user_share_img=ShopConfig::get('website', 'user_share_img');
        $share_name = ShopConfig::get('website', 'share_name');
        $share_content = ShopConfig::get('website', 'share_content');
        $share_img = ShopConfig::get('website', 'share_img');
        $data = [
            'user_share_img' =>$user_share_img,//海报分享图
            'share_name' =>$share_name,//小程序分享标题
            'share_content' =>$share_content,//小程序分享内容
            'share_img' =>$share_img,//小程序分享图片
        ];
        return success($data);
    }

    //公共配置
    public function public_config(Request $request)
    {
        $shop_address = ShopConfig::get('shop', 'address');
        $shop_contact = ShopConfig::get('shop', 'contact');
        $shop_mobile = ShopConfig::get('shop', 'mobile');

        $data = [
            'shop_address' =>$shop_address,//商家地址
            'shop_contact' =>$shop_contact,//商家联系人
            'shop_mobile' =>$shop_mobile,//商家电话
        ];
        return success($data);
    }
    

    //文章详情
    public function wz_config(Request $request)
    {
        $param = $request->param();
        $id=$param['id']??'0';
        $type=$param['type']??'';
        if($id>0){
            $article_info=ArticleLogic::getPolicyAgreementDetail($id);
            $info['name']=$article_info['title'];
            $preg = '/<img.*?src="((?!(https|http)).*?)".*?\/?>/i';
            $local_url = UrlServer::getFileUrl();
            $info['description'] = preg_replace($preg, '<img src="' . $local_url . '${1}" />',$article_info['content']);
        }
        return success($info);
    }

    //上传图片
    public function icon(Request $request)
    {
        $param = $request->param();
        $user_id =$this->uid??0;
        $data = FileServer::userFormImage($user_id);
        if($data['code']==1){
            return success($data['data'],$data['msg']);
        }else{
            return error($data['msg']);
        }
    }
}